package com.gljx.web.controller.JournalController;

import com.gljx.web.Entity.JournalEntry.TrainDeductAccountsJournal;
import com.gljx.web.Entity.Person;
import com.gljx.web.Entity.User;
import com.gljx.web.service.PersonService;
import com.gljx.web.service.TrainDeductAccountsJournalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@RequestMapping("/")
public class TrainDeductAccountsJournalController {
	
	@Autowired
	private TrainDeductAccountsJournalService trainDeductAccountsJournalService;
	
	@Autowired
	private PersonService personService;
	
	@RequestMapping(value="queryTrainDeductAccountsJournal/look.action")
	public String selTrainDeductAccountsJournal(HttpServletRequest request,String beginTime,String endTime,String person)throws Exception{
		//获取当前操作人
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute("user");
		Calendar cale = Calendar.getInstance();// 得到一个Calendar的实例
		cale.add(Calendar.MONTH, 0);
		cale.set(Calendar.DAY_OF_MONTH, 1);
		Date resultDate = cale.getTime(); // 结果
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		if (beginTime == null || beginTime.equals("")) {
			beginTime = sdf.format(resultDate);
		}
		if (endTime == null || endTime.equals("")) {
			endTime = sdf.format(new Date());
		}
		if (person != null) {
			if (person.equals("")) {
				person = null;
			}
		}
		Map<String,String> map =new HashMap<>();
		map.put("companyId", user.getFcompanyid());
		map.put("beginTime", beginTime);
		map.put("endTime", endTime);
		map.put("person", person);
		//Map map;
		List<TrainDeductAccountsJournal>  trainDeductAccountsJournalTwoList=trainDeductAccountsJournalService.selTrainDeductAccountsJournalTwo(map);
		List<TrainDeductAccountsJournal>  trainDeductAccountsJournalThreeList=trainDeductAccountsJournalService.selTrainDeductAccountsJournalThree(map);
		//获取职员
		// 拼接SQL语句
		StringBuffer psql = new StringBuffer();
		psql.append(
				"select T.FID, T.FNUMBER,to_char(t.CFBIRTHDAY,'YYYY-MM-DD ') cfbirthdays,to_char(t.cfstartdate,'YYYY-MM-DD ') cfstartdate,");
		psql.append(
				"to_char(t.cfenddate,'YYYY-MM-DD ') cfenddate,to_char(t.cfbegindate,'YYYY-MM-DD ') cfbegindate,to_char(t.CFHTDQSJ,'YYYY-MM-DD ') cfhtdqsj,");
		psql.append(
				"T.CFIDCARDNO, T.CFCELL, T.CFADDRESS,T.CFPERSONTYPE, T.CFSEX,T.CFXCTZ, T.CFGWYD, T.CFQJJL, T.CFWJJLJL,p.fname_l2 as positiongradeName,t.cfaccountsno, ");
		psql.append(
				"T.CFSHJLJL,T.FNAME_L2,T.CFZSTC,T.CFPXTC,A.FNAME_L2 AS CFPERSONDEPNAME,C.fnumber AS CFCARNAME from ct_ds_person t left join ");
		psql.append(
				"CT_DS_ADMIN A ON A.FID=T.CFPERSONDEPID left join CT_DS_CAR C ON C.FID=T.CFCARID left join CT_DS_POSITIONGRADE p on t.cfjobgradeid=p.fid ");
		List<Person> persons = personService.selectPerson(psql.toString());
		request.setAttribute("persons", persons);
		request.setAttribute("beginTime", beginTime);
		request.setAttribute("endTime", endTime);
		request.setAttribute("person", person);
		request.setAttribute("trainDeductAccountsJournalTwoList", trainDeductAccountsJournalTwoList);
		request.setAttribute("trainDeductAccountsJournalThreeList", trainDeductAccountsJournalThreeList);
		return "backHome/journaling/manageJournaling/trainDeductAccountsJournaling/trainDeductAccountsJournalingList";
	}
}
